package org.dei.perla.rockfall.gui.queryforge;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

import org.dei.perla.sys.device.fpc.dspic.datastructures.MRP;
import org.dei.perla.utils.dataconverter.DataConverter;

public class SessionManager {
	public static boolean saveSession(MRP dataToSave, String fileName){
	
		try {
			Byte[] array = DataConverter.toByteArray(dataToSave);
			FileOutputStream fileOutputStream = new FileOutputStream(new File(fileName));
			byte arrayRaw[] = new byte[array.length];
			for(int i = 0; i < array.length; i++)
				arrayRaw[i] = array[i];
			fileOutputStream.write(arrayRaw);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public static MRP restoreSession(String fileName){
		MRP mrp = null;
		try {
			mrp = new MRP();
			File fin = new File(fileName);
			ParserParamFile.parse(fin, mrp);
		} catch (FilterFileParserException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} 
		return mrp;
	} 
}
